package com.ruoyi.system.service.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.service.*;
import org.apache.ibatis.session.SqlSessionFactory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.enums.CateNameEnum;
import com.ruoyi.common.utils.PqlUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.TreeData;
import com.ruoyi.system.domain.greatWall.CollectionEntity;
import com.ruoyi.system.domain.greatWall.GwRelic;
import com.ruoyi.system.domain.greatWall.RelicCategory;
import com.ruoyi.system.domain.greatWall.Years;
import com.ruoyi.system.mapper.greatWall.GwRelicMapper;
import com.ruoyi.system.mapper.greatWall.YearsMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.SecurityConfig;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 长城三维文物Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-04-23
 */
@Service
public class GwRelicServiceImpl extends ServiceImpl<GwRelicMapper,GwRelic> implements IGwRelicService
{
    @Autowired
    private GwRelicMapper gwRelicMapper;

    @Autowired
    private CollectionService collectionService;

    @Autowired
    private SysDepartService departService;

    @Resource
    private ISysDeptService iSysDeptService;

    @Autowired
    private YearsMapper yearsMapper;

    @Autowired
    private RelicCategoryService categoryService;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;



    /**
     * 查询长城三维文物列表
     * 
     * @param relic 长城三维文物
     * @return 长城三维文物
     */
    @Override
    public List<GwRelic> selectGwRelicList(GwRelic relic)
    {
        QueryWrapper<GwRelic> queryWrapper = new QueryWrapper<>();


        queryWrapper= SecurityUtils.isAdmin(queryWrapper);
        if (SecurityUtils.isAdminNull()){
            queryWrapper.orderByAsc("pql");
        }else {
            queryWrapper.orderByAsc("status");
        }


        if (relic !=null){
            //按照文物名称查询
            if (StringUtils.isNotEmpty(relic.getName())){
                queryWrapper.like("name",relic.getName());
            }

            //按照年代查询
            if (StringUtils.isNotEmpty(relic.getYearsId())){
                queryWrapper.eq("years_id",relic.getYearsId());
            }
            //按照馆藏单位查询
            if(StringUtils.isNotEmpty(relic.getTypeId())){
                queryWrapper.eq("type_id",relic.getTypeId());
            }
            //按照景点id
            if(StringUtils.isNotEmpty(relic.getDepId())){
                queryWrapper.eq("dep_id",relic.getDepId());
            }
            //按照材质查询
            if (StringUtils.isNotEmpty(relic.getMaterialId())){
                queryWrapper.eq("mat_id",relic.getMaterialId());
            }
            if (relic.getDiffStatus() !=null){
                queryWrapper.eq("diff_status",relic.getDiffStatus());
            }
            if (StringUtils.isNotEmpty(relic.getCategoryId())){
                queryWrapper.eq("category_id",relic.getCategoryId());
            }
            //根据审核状态查询
            if (relic.getStatus() !=null){
                if (relic.getStatus() ==20){
                    queryWrapper.eq("status",0).or().eq("status",2);
                }else {
                    queryWrapper.eq("status",relic.getStatus());
                }
            }
        }

        List<GwRelic> relicList = gwRelicMapper.selectList(queryWrapper);

        relicList.forEach(item ->{

            if (item.getDiffStatus() !=null){
                String diffStatusName=item.getDiffStatus() == 1 ? "二维" : "三维";
                item.setDiffStatusName(diffStatusName);
            }

            //馆藏单位名称
            if (StringUtils.isNotEmpty(item.getTypeId())){
                CollectionEntity entity = (CollectionEntity) collectionService.getById(item.getTypeId());
                if (entity !=null) {
                    item.setCollectionName(entity.getName());
                }
            }

            // 设置部门名称
            //String depName = departService.getNameById(item.getDepId());
            SysDept dept = iSysDeptService.selectDeptById(Long.valueOf(item.getDepId()));
            item.setDepName(dept.getDeptName());

            //年代
            Years years=yearsMapper.selectById(item.getYearsId());
            if (years !=null){
                item.setYearName(years.getName());
            }

            //设置类别
/*            RelicCategory category = categoryService.getById(item.getCategoryId());
            if (category !=null && !StringUtils.isEmpty(category.getName())){
                item.setCategoryName(category.getName());
            }*/
            String cateName = CateNameEnum.getName(item.getCategoryId());
            item.setCategoryName(cateName);
        });

        return relicList;
    }



    @Override
    public void setStatus(String id, Integer status) {
        GwRelic relic =gwRelicMapper.selectById(id);
        if (relic !=null){
            relic.setStatus(status);
            updateById(relic);
        }



    }

    @Override
    public Page<GwRelic> recommonList(Page<GwRelic> page, GwRelic relic) {
        QueryWrapper<GwRelic> wrapper=new QueryWrapper<>();
        if (relic !=null){
            if (StringUtils.isNoneEmpty(relic.getId())){
                wrapper.ne("id",relic.getId());
            }
            if (StringUtils.isNotEmpty(relic.getYearsId())){
                wrapper.eq("years_id",relic.getYearsId());
            }
            if (StringUtils.isNotEmpty(relic.getMaterialId())){
                wrapper.eq("mat_id",relic.getMaterialId());
            }
            if (StringUtils.isNotEmpty(relic.getDepId())){
                wrapper.eq("dep_id",relic.getDepId());
            }
            if (StringUtils.isNotEmpty(relic.getCategoryId())){
                wrapper.eq("category_id",relic.getCategoryId());
            }
            if (StringUtils.isNotEmpty(relic.getTypeId())){
                wrapper.eq("type_id",relic.getTypeId());

            }
            if (relic.getDiffStatus() !=null){
                wrapper.eq("diff_status",relic.getDiffStatus());
            }

        }
        wrapper.eq("status",1);
        wrapper.orderByAsc("pql");
        page= gwRelicMapper.selectPage(page,wrapper);
        return page;
    }

    @Override
    public Map<String, List> getInfo() {
        Map<String, List> map = new HashMap<>();



        Map<String, List<Map<String, String>>> info = gwRelicMapper.selectRelicInfo().stream().collect(Collectors.groupingBy(m -> m.get("type")));
        map.putAll(info);
        map.put("pqlId", PqlUtil.pqlList());
        map.put("GwCate",PqlUtil.list());


        List<RelicCategory> list = categoryService.list(
                Wrappers.<RelicCategory>lambdaQuery()
                        .select(RelicCategory::getId, RelicCategory::getParentId, RelicCategory::getName, RelicCategory::getType)
        );
        List relicCategories = TreeData.toTreeList(list);
        map.put("category", relicCategories);
        return map;
    }


}
